<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.search_val" placeholder="搜索代拍" style="width: 400px;margin-right: 10px;" @keyup.enter.native="handleFilter">
        <el-select slot="prepend" v-model="listQuery.search_key" placeholder="请选择">
          <template v-for="item in searchKeys">
            <el-option :key="item.val" :label="item.label" :value="item.val" />
          </template>
        </el-select>
        <el-button slot="append" icon="el-icon-search" @click="handleFilter" />
      </el-input>
      <el-button v-if="envRole === 'super'" class="filter-item" type="primary" @click="handleAdd">添加代拍</el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      fit
      stripe
      border
    >
      <el-table-column type="index" width="60" align="center" />
      <el-table-column key="name" prop="base.name" label="代拍名称" width="190" align="center" show-overflow-tooltip />
      <el-table-column v-if="envRole === 'super'" key="username" prop="base.username" label="账号" width="180" align="center" />
      <el-table-column v-if="envRole === 'super'" key="pasdword" prop="base.password" label="密码" width="160" align="center" />
      <el-table-column key="tel" prop="base.tel" label="电话" width="130" align="center" />
      <el-table-column v-if="envRole === 'sub'" key="remark" prop="buy.remark" label="备注" width="210" align="center" show-overflow-tooltip />
      <el-table-column label="操作" width="130" align="center">
        <template slot-scope="scope">
          <el-button-group>
            <el-button size="mini" type="warning" @click="handleEdit(scope.row)">编辑</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <div v-show="total > 0" class="pagination-container">
      <el-pagination background :current-page="listQuery.page" :page-sizes="[15, 20, 30]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>

    <el-dialog
      :title="textMap[bidcoInfoMode]"
      fullscreen
      center
      :visible.sync="dialogFormVisible"
      @closed="handleDialogClose"
    >
      <bidco-info
        ref="bidcoInfo"
        :mode="bidcoInfoMode"
        :data="currUser"
        :btn-loading="saveLoading"
        @close="dialogFormVisible = false"
        @submit="handleSubmit"
      />
    </el-dialog>
  </div>
</template>

<script>
import BidcoInfo from '../user/components/BidcoInfo'
import { getBidCos, updateBidCo } from '@/api/admin'

export default {
  components: { BidcoInfo },
  data () {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不得低于6位'))
      } else {
        callback()
      }
    }
    return {
      listQuery: {
        page: 1,
        limit: 15,
        search_key: 'name',
        search_val: '',
        subid: null
      },
      list: null,
      total: 0,
      listLoading: true,
      searchKeys: [
        { label: '名称', val: 'name' },
        { label: '手机号', val: 'tel' }
      ],
      bidcoInfoMode: '',
      currUser: null,
      saveLoading: false,
      dialogFormVisible: false,
      textMap: {
        edit: '修改代拍账户',
        add: '添加代拍账户'
      },
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        account: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      envRole: process.env.VUE_APP_ROLE
    }
  },
  created () {
    this.fetchData()
  },
  methods: {
    fetchData () {
      this.listLoading = true
      getBidCos(this.listQuery).then(res => {
        this.list = res.data.list
        this.total = res.data.total
        this.listLoading = false
      }).catch(err => {
        this.listLoading = false
        console.log(err)
      })
    },
    handleAdd () {
      this.bidcoInfoMode = 'add'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.currUser = JSON.parse(JSON.stringify(this.$refs.bidcoInfo.form))
      })
    },
    handleEdit (row) {
      var curr = JSON.parse(JSON.stringify(row))
      const isNum = typeof curr.buy.fee_rate === 'number'
      curr.buy.fee_rate = isNum ? curr.buy.fee_rate * 10000 / 100 : curr.buy.fee_rate
      this.currUser = curr
      this.bidcoInfoMode = 'edit'
      this.dialogFormVisible = true
    },
    handleSubmit (formData) {
      this.saveLoading = true
      updateBidCo(formData).then(res => {
        this.saveLoading = false
        this.dialogFormVisible = false
        this.listQuery.page = 1
        this.fetchData()
        this.$message.success(res.msg)
      }).catch(err => {
        console.log(err)
        this.saveLoading = false
      })
    },
    handleDialogClose () {
      this.$refs.bidcoInfo.$refs['form'].clearValidate()
      this.$refs.bidcoInfo.formData = new FormData()
    },
    handleFilter () {
      this.listQuery.page = 1
      this.fetchData()
    },
    handleSizeChange (val) {
      this.listQuery.limit = val
      this.fetchData()
    },
    handleCurrentChange (val) {
      this.listQuery.page = val
      this.fetchData()
    }
  }
}
</script>
<style lang="scss" scoped>
.el-table {
  margin-bottom: 20px;
}
.el-form {
  width: 100%;
  max-width: 450px;
  margin: auto;
}
</style>

